package com.ding.easycontroller.file.impl;

import com.ding.easycontroller.file.FilterFile;
import com.ding.easycontroller.file.ScriptFilePathHolder;
import com.ding.easycontroller.file.ServiceFile;
import com.ding.easycontroller.utils.ElementUtils;
import com.ding.easycontroller.work.Filter;
import com.ding.easycontroller.work.Service;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

@Component
public class FilterFileImpl implements FilterFile {

    @Autowired
    private XMLOutputter docWriter;

    private File root;


    FilterFileImpl(ScriptFilePathHolder holder) {
        File parent = holder.getRoot();
        File file = new File(parent, "filter");
        if (!file.exists() && !file.mkdirs()) {
            throw new RuntimeException("路径创建失败:" + file.getAbsolutePath());
        }
        this.root = file;
    }

    private Document filter2Document(Filter filter) {
        Document document = new Document();

        Element root = new Element("filter");

        //基础恢复
        ScriptFile.scriptToElement(filter, root);

        //服务名称
        Element order = new Element("order");
        order.setText(filter.getOrder() + "");
        root.getChildren().add(order);

        if (filter.getBeforeFunctionName() != null) {
            Element beforeFunctionName = new Element("beforeFunctionName");
            beforeFunctionName.setText(filter.getBeforeFunctionName());
            root.getChildren().add(beforeFunctionName);
        }

        if (filter.getDefaultFunctionName() != null) {
            Element defaultFunctionName = new Element("defaultFunctionName");
            defaultFunctionName.setText(filter.getDefaultFunctionName());
            root.getChildren().add(defaultFunctionName);
        }

        if (filter.getAfterFunctionName() != null) {
            Element afterFunctionName = new Element("afterFunctionName");
            afterFunctionName.setText(filter.getAfterFunctionName());
            root.getChildren().add(afterFunctionName);
        }

        document.setRootElement(root);

        return document;
    }

    @Override
    public void add(Filter filter) {

        try {
            Document document = filter2Document(filter);
            String strFromXml = docWriter.outputString(document);
            FileWriter writer = new FileWriter(new File(this.root, filter.getId() + ".filter"));
            writer.write(strFromXml);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(String id) {
        if (!new File(root, id + ".filter").delete()) {
            throw new RuntimeException("filter移除失败:" + id);
        }
    }

    @Override
    public List<Filter> listAll() {
        List<Filter> all = new ArrayList<>();
        File[] files = root.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".filter")) {
                    Element element = ElementUtils.readElement(file);
                    if (element != null && element.getName().equals("filter")) {
                        Filter filter = new Filter();
                        ScriptFile.elementToScript(element, filter);
                        filter.setOrder(Integer.parseInt(element.getChild("order").getText()));

                        Element beforeFunctionName = element.getChild("beforeFunctionName");
                        if (beforeFunctionName != null) {
                            filter.setBeforeFunctionName(beforeFunctionName.getText());
                        }

                        Element afterFunctionName = element.getChild("afterFunctionName");
                        if (afterFunctionName != null) {
                            filter.setAfterFunctionName(afterFunctionName.getText());
                        }

                        Element defaultFunctionName = element.getChild("defaultFunctionName");
                        if (defaultFunctionName != null) {
                            filter.setDefaultFunctionName(defaultFunctionName.getText());
                        }

                        all.add(filter);
                    }
                }
            }
        }
        return all;
    }

    @Override
    public void update(Filter filter) {
        try {
            Document document = filter2Document(filter);
            String strFromXml = docWriter.outputString(document);
            FileWriter writer = new FileWriter(new File(this.root, filter.getId() + ".filter"));
            writer.write(strFromXml);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
